package com.example.Android_test.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.Android_test.R;
import com.example.Android_test.helpers.SOWEngine;
import com.example.Android_test.helpers.UniqViewIDHelper;
import com.example.Android_test.specs.SetOfWordsSpec;
import com.example.Android_test.specs.WordSpec;
import com.example.Android_test.util.MyTextView;
import com.example.Android_test.util.PlaySound;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: anpi0413
 * Date: 04.06.13
 * Time: 16:54
 * To change this template use File | Settings | File Templates.
 */
public class SetOfWordOverview extends Activity {
    public static final String TITLE = "SelectSetOfWords";

    private LinearLayout scrolledLayout;
    private String currentSetID = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.other_setofwordoverview);
        this.setTitle("SetOfWordOverview");

        scrolledLayout = (LinearLayout) findViewById(R.id.sow_scrollView);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            currentSetID = bundle.getString("id");
        }

        fillLayoutFromSet();
        // Setting the RelativeLayout as our content view
      //  setContentView(relativeLayout, rlp);
    }

    private void fillLayoutFromSet() {
        scrolledLayout.removeAllViews();

        SetOfWordsSpec setOfWords;
        try {
            if (currentSetID != null) {
                setOfWords = SOWEngine.INSTANCE.getSetOfWordByID(currentSetID);
            } else {
                setOfWords = SOWEngine.INSTANCE.getSelectedSetOfWord();
            }
        } catch (Exception e) {
            Log.e(Main.LOG_TAG, "SelectTraining.fillLayoutFromSet ERROR " + e.getMessage());
            return;
        }

        ArrayList<WordSpec> words = setOfWords.getWords();

        RelativeLayout titleRelativeLayout = new RelativeLayout(this);
        titleRelativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));

        MyTextView titleTextView = new MyTextView(this);
        titleTextView.setMaxLines(10);
        titleTextView.setText(Html.fromHtml("<b>" + setOfWords.getSowComment() + "</b>"));

        titleRelativeLayout.addView(titleTextView);

        scrolledLayout.addView(titleRelativeLayout);

        //int id = 100500;
        for (WordSpec currentWord : words){
            // Creating a new RelativeLayout
            RelativeLayout relativeLayout = new RelativeLayout(this);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            relativeLayout.setLayoutParams(rlp);

            // Creating a new TextView
            MyTextView myTextView = new MyTextView(this);
            myTextView.setId(UniqViewIDHelper.INSTANCE.getUniqueId());

            myTextView.setMaxLines(10);
            myTextView.setText(Html.fromHtml("<b>" + currentWord.getWord() + "</b>" + " " + currentWord.getTranscription() + " <br/>" +
                    currentWord.getTranslationVariantSpecs().get(0).getTranslate()));

            relativeLayout.addView(myTextView);


            TextView descriptionView = new TextView(this);
            descriptionView.setText(Html.fromHtml("<i>" + currentWord.getInContext() + "</i>"));
            descriptionView.setId(UniqViewIDHelper.INSTANCE.getUniqueId());


            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.BELOW, myTextView.getId());

            descriptionView.setLayoutParams(lp);

            // Adding the TextView to the RelativeLayout as a child
            relativeLayout.addView(descriptionView);

            if (currentWord.getSoundResName() != null && !currentWord.getSoundResName().isEmpty()) {
                RelativeLayout.LayoutParams lpForPlayButton = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                lpForPlayButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                lpForPlayButton.addRule(RelativeLayout.ALIGN_PARENT_TOP);

                Button buttonPlay = new Button(this);
                buttonPlay.setText("Play");
                buttonPlay.setTag(currentWord.getSoundResName());
                buttonPlay.setLayoutParams(lpForPlayButton);
                buttonPlay.setOnClickListener(new ButtonPlayListener());
                relativeLayout.addView(buttonPlay);
            }



            View v = new View(this);
            v.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 1));
            v.setBackgroundColor(getResources().getColor(R.color.separate_line_in_lists));

            relativeLayout.addView(v);

            scrolledLayout.addView(relativeLayout);
        }
    }

    private class ButtonPlayListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            new PlaySound((String) v.getTag()).play();
        }
    }

}